package edu.nf.waimai.dao.user.impl;

import edu.nf.util.DBUtils;
import edu.nf.waimai.dao.user.UserDAO;
import edu.nf.waimai.model.user.Address;
import edu.nf.waimai.model.user.User;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

/**
 * @Date 2023-06-19
 * @Author qiu
 */
public class UserDaoImpl implements UserDAO {

    /**
     * 用户登录
     * @param username
     * @return
     */
    @Override
    public User loginUser(String username) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());

        String sql = "select u_id,u_name,u_pwd,u_nickname,u_phone,u_avatar from users where u_name = ?";

        BeanHandler<User> userBeanHandler = new BeanHandler<>(User.class);

        return sqlExecutor.executeQuery(sql, userBeanHandler, username);
    }

    /**
     * 用户注册
     * @param uName
     * @param pwd
     * @param nickName
     * @param uPhone
     * @return
     */
    @Override
    public int registerUser(String uName, String pwd, String nickName, String uPhone) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "insert into users(u_name,u_pwd,u_nickname,u_phone) values(?,?,?,?)";

        return sqlExecutor.executeUpdate(sql, uName, pwd, nickName, uPhone);
    }

    /**
     * 查询手机号是否注册
     * @param phone
     * @return
     */
    @Override
    public User equeryUserPhone(String phone) {

        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());

        String sql = "select * from users where u_phone = ?";

        BeanHandler<User> userBeanHandler = new BeanHandler<>(User.class);

        return sqlExecutor.executeQuery(sql, userBeanHandler, phone);
    }

    @Override
    public User loginUserPhone(String phone) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());

        String sql = "select u_name,u_pwd,u_nickname,u_phone,u_avatar from users where u_phone = ? ";

        BeanHandler<User> userBeanHandler = new BeanHandler<>(User.class);

        return sqlExecutor.executeQuery(sql,userBeanHandler,phone);
    }

    @Override
    public List<Address> queryListUserAddress(Integer uid) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT address_id,u_id,address_name,address_phone,province,city,district,detail,areaCode,is_default,address_datetime FROM address WHERE u_id = ?;";
        return sqlExecutor.executeQuery(sql,new BeanListHandler<>(Address.class),uid);
    }

    @Override
    public Address queryUserAddressById(Integer addressId) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT address_id,u_id,address_name,address_phone,province,city,district,detail,areaCode,is_default,address_datetime FROM address WHERE address_id = ?;";
        return sqlExecutor.executeQuery(sql,new BeanHandler<>(Address.class),addressId);
    }

    @Override
    public int insertAddress(Address address) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = " INSERT INTO address (u_id, address_name, address_phone, province, city, district, detail, areaCode, is_default) " +
                "VALUES (?,?,?,?,?,?,?,?,?);";
        return sqlExecutor.executeUpdate(sql,
                address.getUid(),
                address.getName(),
                address.getTel(),
                address.getProvince(),
                address.getCity(),
                address.getCounty(),
                address.getAddressDetail(),
                address.getAreaCode(),
                address.getIsDefault());
    }

    @Override
    public int updateAddress(Address address) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "UPDATE address SET address_name = ?, address_phone = ?, province = ?, city = ?, district = ?, detail = ?, areaCode = ?, is_default = ? " +
                "WHERE address_id = ?;";
        return sqlExecutor.executeUpdate(sql,
                address.getName(),
                address.getTel(),
                address.getProvince(),
                address.getCity(),
                address.getCounty(),
                address.getAddressDetail(),
                address.getAreaCode(),
                address.getIsDefault(),
                address.getId());
    }

    @Override
    public Address getUserAddress(Address address) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        StringBuilder sql = new StringBuilder(" SELECT address_id,u_id,address_name,address_phone,province,city,district,detail,areaCode,is_default,address_datetime FROM address \n" +
                " WHERE u_id = ? ");
        if (address.getId() != null) {
            sql.append(" AND address_id = ? ");
        }
        sql.append(" AND is_default = ?;");

        return address.getId() == null ?
            sqlExecutor.executeQuery(sql.toString(),new BeanHandler<>(Address.class),
                address.getUid(),
                address.getIsDefault())
         :
            sqlExecutor.executeQuery(sql.toString(),new BeanHandler<>(Address.class),
                address.getUid(),
                address.getId(),
                address.getIsDefault());

    }
}
